class GroleController {


	static roleAccess =  [
		[action:["*"], roles:["admin"]],
	]

    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [save:'POST', update:'POST']

    def list = {
        if(!params.max) params.max = 10
        [ groleList: Grole.list( params ) ]
    }

    def show = {
        def grole = Grole.get( params.id )

        if(!grole) {
            flash.message = "Grole not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ grole : grole ] }
    }

    def delete = {
        def grole = Grole.get( params.id )
        if(grole) {
            grole.delete()
            flash.message = "Grole ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Grole not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def grole = Grole.get( params.id )

        if(!grole) {
            flash.message = "Grole not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ grole : grole ]
        }
    }

    def update = {
        def grole = Grole.get( params.id )
        if(grole) {
            grole.properties = params

		params.findAll{pm-> 
			pm.key.length()>2 && pm.key[0..2]=="otm"
		}.each {  p->
			def keys = p.key.split("_");
			def classkey = keys[1]
			def addtokeyorig = keys[2]
			def addtokey = 'addTo' + addtokeyorig[0].toUpperCase() + addtokeyorig[1..-1]
			classkey = classkey[0].toUpperCase() + classkey[1..-1]
		
			grole."$addtokeyorig"= null

			p.value.each { val->
				def temp = Class.forName(classkey, true, grailsApplication.classLoader).newInstance()
				temp = temp.get(val)
				grole."$addtokey"(temp)			
			} 
		}


            if(!grole.hasErrors() && grole.save()) {
                flash.message = "Grole ${params.id} updated"
                redirect(action:show,id:grole.id)
            }
            else {
                render(view:'edit',model:[grole:grole])
            }
        }
        else {
            flash.message = "Grole not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def grole = new Grole()
        grole.properties = params
        return ['grole':grole]
    }

    def save = {
        def grole = new Grole(params)


	params.findAll{pm-> 
		pm.key.length()>2 && pm.key[0..2]=="otm"
	}.each {  p->
		def keys = p.key.split("_");
		def classkey = keys[1]
		def addtokeyorig = keys[2]
		def addtokey = 'addTo' + addtokeyorig[0].toUpperCase() + addtokeyorig[1..-1]
		classkey = classkey[0].toUpperCase() + classkey[1..-1]
	
		Grole."$addtokeyorig"= null

		p.value.each { val->
			def temp = Class.forName(classkey, true, grailsApplication.classLoader).newInstance()
			temp = temp.get(val)
			Grole."$addtokey"(temp)			
		} 
	}


        if(!grole.hasErrors() && grole.save()) {
            flash.message = "Grole ${grole.id} created"
            redirect(action:show,id:grole.id)
        }
        else {
            render(view:'create',model:[grole:grole])
        }
    }
}
